package top.yongyan0624.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import top.yongyan0624.common.lang.CodeMsg;
import top.yongyan0624.common.lang.ResultVO;
import top.yongyan0624.entity.*;
import top.yongyan0624.service.MessageService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yongyan
 * @since 2021-05-17
 */
@RestController
public class MessageController extends BaseController {

    @Autowired
    private MessageService messageService;

    @PostMapping("/leavingMessage/message")
    public ResultVO getMessage(@RequestParam(required = false) Long postId, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Page<Message> pageData = messageService.getMessage(postId,pageNum,pageSize);

        return ResultVO.success(pageData);
    }

    @PostMapping("/leavingMessage/getMessageById")
    public ResultVO getMessageById(@RequestParam(required = false) Long postId, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Page<Message> pageData = messageService.getMessageById(postId,pageNum,pageSize);

        return ResultVO.success(pageData);
    }

    @PostMapping("/leavingMessage/submitMessage")
    public ResultVO submitMessage(@RequestBody Message message) {
        message.setTime(LocalDateTime.now());

        if (messageService.save(message)) {
            return ResultVO.success("感谢留言！");
        } else {
            return ResultVO.error(new CodeMsg("留言出错啦！"));
        }
    }

    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @PostMapping("/admin/message")
    public ResultVO queryMessage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Page<Message> pageData = messageService.getMessage(null,pageNum,pageSize);

        return ResultVO.success(pageData);
    }

    @PreAuthorize("hasRole('superAdmin')")
    @Transactional
    @RequestMapping("/admin/delMessage/{id}")
    public ResultVO delMessage(@PathVariable Long id) {
        boolean ok = messageService.removeById(id);

        if (ok) {
            return ResultVO.success("删除成功！");
        }

        return ResultVO.error(new CodeMsg("对不起！不存在这条留言，删除失败！"));
    }

    @PreAuthorize("hasRole('superAdmin')")
    @Transactional
    @RequestMapping("/admin/delAllMessage")
    public ResultVO delAllMessage(@RequestBody MyList<Long> myList) {
        List<Long> ids = myList.getList();
        for (Long id : ids) {
            if (delMessage(id).getCode() == 400) {
                return ResultVO.error(new CodeMsg("对不起！不存在这条留言:" + id + "，删除失败！"));
            }
        }
        return ResultVO.success("删除成功！");
    }

    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @RequestMapping("/admin/queryMessageByContent")
    public ResultVO queryUserByUsername(@RequestParam String content, @RequestParam Long pageNum, @RequestParam Long pageSize) {
        Page<Message> page = new Page<>(pageNum, pageSize);
        Page<Message> pageData = messageService.page(page, new QueryWrapper<Message>()
                .like(content != null && !content.equals(""), "message", content)
                .orderByDesc("time"));
        messageService.messageCache(null,pageData);

        return ResultVO.success(pageData);
    }

    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @RequestMapping("/admin/countMessage")
    public ResultVO countMessage() {
        return ResultVO.success(messageService.count());
    }
}
